package chain_user_config

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/repository/mongodb/user"
	"chain_bridge_service/internal/services/chain_user_config"
	"net/http"
)

type createResult struct {
	Code int    `json:"code"`
	Data Page   `json:"data"`
	Msg  string `json:"msg" `
}

type request struct {
	PageIndex uint `form:"pageIndex"` // 第几页
	PageSize  uint `form:"pageSize"`  // 每页显示条数
}

type Page struct {
	Total int64                                   `json:"total"`
	List  []chain_user_config.ChainUserConfigData `json:"list"`
}

func (h *handler) GetListByUserId() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(request)
		res := new(createResult)
		if err := ctx.ShouldBindQuery(&req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		userInfo := ctx.GetInfo("userInfo").(*user.User)
		count, configList, err := h.chainUserConfigService.GetConfigByUserId(ctx, userInfo.Id, req.PageIndex, req.PageSize)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetConfigError,
				code.Text(code.GetConfigError)).WithError(err),
			)
			return
		}
		res.Code = code.StatusOK
		res.Data = Page{Total: count, List: configList}
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
